package hlp.bo;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;

import hlp.dao.PostDao;
import hlp.hibernateUtils.HibernateUtil;
import hlp.hibernateUtils.TransactionFactory;
import hlp.model.Post;

public class PostManager {

	private PostDao postDao = new PostDao();
	
	public Long savePost(String user, String userDescription, String text){
		Post post = new Post();
		post.setUser(user);
		post.setUserDescription(userDescription);
		post.setText(text);
		post.setDate(new Date(System.currentTimeMillis()));
		Transaction tx = TransactionFactory.begin();
		Long id = postDao.save(post);
		tx.commit();
		return id;
	}
	
	public List<Post> getPosts(){
		TransactionFactory.begin();
		Query q = HibernateUtil.currentSession().createQuery("from " + Post.class.getName());
		return q.list();
	}
}
